namespace DotNetNuke.UI.WebControls
{
    using DotNetNuke.UI.Utilities;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class DNNLabelEdit : Label, IPostBackEventHandler, IClientAPICallbackEventHandler
    {
        public event DNNLabelEditEventHandler UpdateLabel;

        public DNNLabelEdit()
        {
            base.PreRender += new EventHandler(this.DNNLabelEdit_PreRender);
        }

        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            base.AddAttributesToRender(writer);
            if (this.EditEnabled)
            {
                writer.AddAttribute("sysimgpath", this.SystemImagesPath);
                if (Strings.Len(this.LabelEditCssClass) > 0)
                {
                    writer.AddAttribute("cssEdit", this.LabelEditCssClass);
                }
                if (Strings.Len(this.WorkCssClass) > 0)
                {
                    writer.AddAttribute("cssWork", this.WorkCssClass);
                }
                if (Strings.Len(this.MouseOverCssClass) > 0)
                {
                    writer.AddAttribute("cssOver", this.MouseOverCssClass);
                }
                if (StringType.StrCmp(this.EventName, "onclick", false) != 0)
                {
                    writer.AddAttribute("eventName", this.EventName);
                }
                if (this.MultiLine)
                {
                    writer.AddAttribute("multiline", "1");
                }
                if (this.RichTextEnabled)
                {
                    writer.AddAttribute("richtext", "1");
                }
                if (ClientAPI.BrowserSupportsFunctionality(ClientAPI.ClientFunctionality.XMLHTTP))
                {
                    writer.AddAttribute("callback", ClientAPI.GetCallbackEventReference(this, "'[TEXT]'", "this.callBackSuccess", "this", "this.callBackFail", "this.callBackStatus"));
                }
                if (Strings.Len(this.CallbackStatusFunction) > 0)
                {
                    writer.AddAttribute("callbackSF", this.CallbackStatusFunction);
                }
            }
        }

        private void DNNLabelEdit_PreRender(object sender, EventArgs e)
        {
            this.RegisterClientScript();
        }

        protected override void LoadViewState(object state)
        {
            if (state != null)
            {
                object[] objArray = (object[]) state;
                if (objArray[0] != null)
                {
                    base.LoadViewState(RuntimeHelpers.GetObjectValue(objArray[0]));
                }
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
        }

        public string RaiseClientAPICallbackEvent(string eventArgument)
        {
            if (this.UpdateLabel != null)
            {
                this.UpdateLabel(this, new DNNLabelEditEventArgs(eventArgument));
            }
            return eventArgument;
        }

        public virtual void RaisePostBackEvent(string eventArgument)
        {
            if (this.UpdateLabel != null)
            {
                this.UpdateLabel(this, new DNNLabelEditEventArgs(eventArgument));
            }
        }

        public void RegisterClientScript()
        {
            if (!this.IsDownLevel && this.EditEnabled)
            {
                ClientAPI.RegisterClientReference(this.Page, ClientAPI.ClientNamespaceReferences.dnn_dom);
                ClientAPI.RegisterClientReference(this.Page, ClientAPI.ClientNamespaceReferences.dnn_xml);
                ClientAPI.RegisterClientReference(this.Page, ClientAPI.ClientNamespaceReferences.dnn_dom_positioning);
                ClientAPI.RegisterClientReference(this.Page, ClientAPI.ClientNamespaceReferences.dnn_xmlhttp);
                if (!ClientAPI.IsClientScriptBlockRegistered(this.Page, "dnn.controls.dnnlabeledit.js"))
                {
                    ClientAPI.RegisterClientScriptBlock(this.Page, "dnn.controls.dnnlabeledit.js", "<script src=\"" + this.LabelEditScriptPath + "dnn.controls.dnnlabeledit.js\"></script>");
                }
                ClientAPI.RegisterStartUpScript(this.Page, this.ClientID + "_startup", "<script>dnn.controls.initLabelEdit(dnn.dom.getById('" + this.ClientID + "'));</script>");
            }
        }

        protected override object SaveViewState()
        {
            object objectValue = RuntimeHelpers.GetObjectValue(base.SaveViewState());
            return new object[] { RuntimeHelpers.GetObjectValue(objectValue) };
        }

        protected override void TrackViewState()
        {
            base.TrackViewState();
        }

        [PersistenceMode(PersistenceMode.Attribute), Bindable(true)]
        public string CallbackStatusFunction
        {
            get
            {
                return StringType.FromObject(this.ViewState["CallbackStatusFunction"]);
            }
            set
            {
                this.ViewState["CallbackStatusFunction"] = value;
            }
        }

        public string ClientAPIScriptPath
        {
            get
            {
                return ClientAPI.ScriptPath;
            }
            set
            {
                ClientAPI.ScriptPath = value;
            }
        }

        [DefaultValue(true), PersistenceMode(PersistenceMode.Attribute)]
        public bool EditEnabled
        {
            get
            {
                if (Strings.Len(RuntimeHelpers.GetObjectValue(this.ViewState["EditEnabled"])) > 0)
                {
                    return BooleanType.FromObject(this.ViewState["EditEnabled"]);
                }
                return true;
            }
            set
            {
                this.ViewState["EditEnabled"] = value;
            }
        }

        [PersistenceMode(PersistenceMode.Attribute), Bindable(true), DefaultValue("onclick")]
        public string EventName
        {
            get
            {
                if (Strings.Len(RuntimeHelpers.GetObjectValue(this.ViewState["EventName"])) == 0)
                {
                    return "onclick";
                }
                return StringType.FromObject(this.ViewState["EventName"]);
            }
            set
            {
                this.ViewState["EventName"] = value;
            }
        }

        [Browsable(false)]
        public bool IsDownLevel
        {
            get
            {
                return ((!this.EditEnabled || !ClientAPI.BrowserSupportsFunctionality(ClientAPI.ClientFunctionality.DHTML)) || !ClientAPI.BrowserSupportsFunctionality(ClientAPI.ClientFunctionality.XML));
            }
        }

        [Bindable(true), PersistenceMode(PersistenceMode.Attribute), DefaultValue("")]
        public string LabelEditCssClass
        {
            get
            {
                return StringType.FromObject(this.ViewState["LabelEditCssClass"]);
            }
            set
            {
                this.ViewState["LabelEditCssClass"] = value;
            }
        }

        public string LabelEditScriptPath
        {
            get
            {
                if (Strings.Len(RuntimeHelpers.GetObjectValue(this.ViewState["LabelEditScriptPath"])) == 0)
                {
                    return this.ClientAPIScriptPath;
                }
                return StringType.FromObject(this.ViewState["LabelEditScriptPath"]);
            }
            set
            {
                this.ViewState["LabelEditScriptPath"] = value;
            }
        }

        [PersistenceMode(PersistenceMode.Attribute), DefaultValue("")]
        public string MouseOverCssClass
        {
            get
            {
                return StringType.FromObject(this.ViewState["MouseOverCssClass"]);
            }
            set
            {
                this.ViewState["MouseOverCssClass"] = value;
            }
        }

        [PersistenceMode(PersistenceMode.Attribute), DefaultValue(false)]
        public bool MultiLine
        {
            get
            {
                if (Strings.Len(RuntimeHelpers.GetObjectValue(this.ViewState["MultiLine"])) == 0)
                {
                    return false;
                }
                return BooleanType.FromObject(this.ViewState["MultiLine"]);
            }
            set
            {
                this.ViewState["MultiLine"] = value;
            }
        }

        [PersistenceMode(PersistenceMode.Attribute), DefaultValue(false)]
        public bool RichTextEnabled
        {
            get
            {
                if (Strings.Len(RuntimeHelpers.GetObjectValue(this.ViewState["RichTextEnabled"])) == 0)
                {
                    return false;
                }
                return BooleanType.FromObject(this.ViewState["RichTextEnabled"]);
            }
            set
            {
                this.ViewState["RichTextEnabled"] = value;
            }
        }

        [DefaultValue("images/"), Description("Directory to find the images for the LabelEdit.  Need to have spacer.gif here!")]
        public string SystemImagesPath
        {
            get
            {
                if (Strings.Len(RuntimeHelpers.GetObjectValue(this.ViewState["SystemImagesPath"])) == 0)
                {
                    return "images/";
                }
                return StringType.FromObject(this.ViewState["SystemImagesPath"]);
            }
            set
            {
                this.ViewState["SystemImagesPath"] = value;
            }
        }

        [DefaultValue(""), Bindable(true), PersistenceMode(PersistenceMode.Attribute)]
        public string WorkCssClass
        {
            get
            {
                return StringType.FromObject(this.ViewState["WorkCssClass"]);
            }
            set
            {
                this.ViewState["WorkCssClass"] = value;
            }
        }

        public delegate void DNNLabelEditEventHandler(object source, DNNLabelEditEventArgs e);
    }
}

